﻿using nhCore.Modbus;
using System;
using System.Collections.Generic;

namespace nhCore
{
    /// <summary>
    /// 记录仪设备，有历史数据，需对历史数据同步
    /// </summary>
    public class RecorderDevcie : BaseDevice
    {
        /// <summary>
        /// 设备id，云链接时使用
        /// </summary>
        public byte Id { get; set; }

        #region 判断是否在线
        /// <summary>
        /// 记录仪支持多条命令，在这记录最后通讯时间
        /// </summary>
        private DateTime LastCommTime;

        /// <summary>
        /// 记录最后一次通讯时间
        /// </summary>
        public void RecordingCommTime()
        {
            LastCommTime = DateTime.Now;
        }

        internal override void AuotAddDecoder()
        {
            throw new NotImplementedException();
        }

        public override List<CmdFrame> GetSentCmds(byte code = 0x04, int workDelayTime = 1000, int sleepDelayTime = 1000)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 判断设备是否在线
        /// </summary>
        public bool IsOnline => DateTime.Now - LastCommTime < CoreGlobal.OutTime;

        /// <summary>
        /// 在线、离线
        /// </summary>
        public string OnlineState => IsOnline ? "在线" : "离线";
        #endregion

        /// <summary>
        /// 每台设备对应的同步状态
        /// </summary>
        public SyncState SyncState { get; set; } = new SyncState();
    }

    /// <summary>
    /// 同步状态
    /// </summary>
    public class SyncState
    {
        private static readonly int maxFailedSize = 5;

        public int SyncFailedCounts { get; set; }
        public int SyncCounts { get; set; }

        /// <summary>
        /// 判断是否同步失败
        /// </summary>
        /// <returns></returns>
        public bool SyncFaileState()
        {
            return SyncFailedCounts >= maxFailedSize;
        }
        /// <summary>
        /// 设置同步失败
        /// </summary>
        public void SetSyncFailedCountsMax()
        {
            SyncFailedCounts += maxFailedSize;
        }
    }
}
